//
//  STShowColListView.m
//  YiYueHairdresser
//
//  Created by zhouzhouzhou on 2018/10/10.
//  Copyright © 2018年 YuYiLian. All rights reserved.
//

#import "STShowColListView.h"
@implementation STShowColListView


-(id)initWithFrame:(CGRect)frame{
    self=[super initWithFrame:frame];
    collectionFrame=frame;
    self.backgroundColor=[UIColor clearColor];
    return self;
}

-(void)setShowCollectionViewWithData:(id)data{
    arrShows = data;
    [showCollectionView.mj_footer endRefreshing];
    [showCollectionView.mj_header endRefreshing];
    [self loadCollectionView];
}

-(void)loadCollectionView{
    float viewW =self.frame.size.width;
    float viewH =self.frame.size.height;
   
    
    if(!showCollectionView){
        flowLayout =[[STAlignmentFlowLayout alloc]init];
        flowLayout.itemSize=self.cellSize;
        flowLayout.sectionInset=self.sectionInset;
        if (self.alimentType == ZYHShowCollectionTypeAlimentRight) {
            flowLayout.spaceNum = 10;
        }
        if (self.horizontalInterval) {
            flowLayout.minimumInteritemSpacing = self.horizontalInterval;//左右间距
        }else{
            flowLayout.minimumInteritemSpacing = 5;
        }
        if (self.verticalInterval) {
            flowLayout.minimumLineSpacing = self.verticalInterval;//左右间距
        }else{
            flowLayout.minimumLineSpacing = 5;
        }
        showCollectionView.dataSource = self;
        showCollectionView=[[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, viewW, viewH) collectionViewLayout:flowLayout];
        Class ownCollectionClass = NSClassFromString(self.showViewClassName);
        [showCollectionView registerClass:[ownCollectionClass class] forCellWithReuseIdentifier:self.showViewClassName];
        showCollectionView.delegate = self;
        showCollectionView.dataSource = self;
        //    showCollectionView.scrollEnabled=NO;
        showCollectionView.showsHorizontalScrollIndicator = NO;
        showCollectionView.showsVerticalScrollIndicator = NO;
        showCollectionView.backgroundColor = [UIColor clearColor];
        WeakSelf
        showCollectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            if ([weakSelf.delegate respondsToSelector:@selector(showColListSlideDown:)]) {
                [weakSelf.delegate showColListSlideDown:weakSelf];
            }
        }];
        showCollectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            if ([weakSelf.delegate respondsToSelector:@selector(showColListSlideUp:)]) {
                [weakSelf.delegate showColListSlideUp:weakSelf];
            }
        }];
        
        
        [self addSubview:showCollectionView];
    }else{
        showCollectionView.frame = self.bounds;
        [showCollectionView reloadData];
    }
}

#pragma mark -collectionViewDelegate


//- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
//    return self.arrShows.count;
//}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return arrShows.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    float viewW =collectionFrame.size.width;
    if(self.alimentType == ZYHShowCollectionTypeAlimentCenter){
        if (arrShows.count && ((viewW - (self.cellSize.width * arrShows.count))>self.cellSize.width)) {
            float w = (viewW-(arrShows.count * 10+1)) / arrShows.count;
            self.cellSize = CGSizeMake(w, self.cellSize.height);
        }
    }
    
    return self.cellSize;
}

////左右间距
//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
//    if(self.verticalInterval){
//        return self.verticalInterval;
//    }
//    return 5;
//}
//
////上下间距
//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout
//minimumLineSpacingForSectionAtIndex:(NSInteger)section{
//    if(self.horizontalInterval){
//        return self.horizontalInterval;
//    }
//    return 5;
//}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSString *identifier = self.showViewClassName;
     Class ownCollectionClass = NSClassFromString(self.showViewClassName);
     UIView * cell = [[ownCollectionClass alloc]init];;
    cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    id showDic=arrShows[indexPath.row];
    [cell performSelector:@selector(setOwnCollectionCellWithData:) withObject:showDic];
    cell.layer.cornerRadius = 5;
    cell.layer.masksToBounds = YES;

    return (UICollectionViewCell *)cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.delegate respondsToSelector:@selector(showColList:SelRow:)]) {
        [self.delegate showColList:self SelRow:indexPath];
    }
}

- (UIView *) hitTest:(CGPoint) point withEvent:(UIEvent *)event {
    UIView * view =[super hitTest:point withEvent:event];
    static BOOL isResponse = 0;
    if(self.isResponseClick){
        UICollectionViewCell * cell = (UICollectionViewCell *)view.superview.superview;
        
        if ([cell isKindOfClass: [UICollectionViewCell class]]) {
            if (isResponse == 0) {
                if ([self.delegate respondsToSelector:@selector(showColList:SelRow:)]) {
                    NSIndexPath * indexPath = [showCollectionView indexPathForCell:cell];
                    [self.delegate showColList:self SelRow:indexPath];
                    isResponse = 1;
                }
            }else{
                isResponse = 0;
            }
        }
    }
    return view;
}




@end
